﻿using System;
using System.Threading;
using System.Web;

namespace Codemonk.Updater.Server
{
    internal class HttpClient : IAsyncResult, IClient
    {
        private HttpContext context;
        public HttpContext HttpContext
        {
            get { return context; }
            set
            {
                if (value == null) throw new NullReferenceException();
                context = value;
                FileName = context.Request.QueryString["file"];
                AppID = context.Request.QueryString["appid"];
            }
        }

        public object AsyncState { get { return null; } }

        public WaitHandle AsyncWaitHandle { get { return null; } }

        public bool CompletedSynchronously { get { return false; } }

        public AsyncCallback Callback { get; set; }

        private bool isCompleted;
        public bool IsCompleted
        {
            get { return isCompleted; }
            private set
            {
                isCompleted = value;
                if (isCompleted)
                {
                    try
                    {
                        if (Callback != null)
                            Callback(this);
                    }
                    catch { }
                }
            }
        }

        private System.IO.Stream stream;
        public System.IO.Stream Stream
        {
            get
            {
                return stream;
            }
            set
            {
                stream = value;
                IsCompleted = true;
            }
        }

        public string FileName { get; set; }

        public string AppID { get; set; }
    }
}
